﻿Imports System.Xml

Namespace Spreads
	Public Class Page
		Inherits Child

		Private Property PageColor As PageColor

		Private Property GridDataInformation As GridDataInformation

		Public Overrides Function ToString() As String
			Return "Page - " & Me.Self
		End Function

		Property Self As String

		Property GeometricBounds As String

		Property ItemTransform As String

		Property Name As String

		Property AppliedTrapPreset As String

		Property OverrideList As String

		Property AppliedMaster As String

		Property MasterPageTransform As String

		Property TabOrder As String()

		Property GridStartingPoint As GridStartingPointOptions

		Property UseMasterGrid As Boolean

		Property MarginPreference As MarginPreference

		Public Property Descriptor As Descriptor

		Public Shared Function ReadXml(reader As XmlReader) As Page
			Dim p As New Page()

			If reader.HasAttributes Then
				p.Self = reader.GetAttribute("Self")
				p.GeometricBounds = reader.GetAttribute("GeometricBounds")
				p.ItemTransform = reader.GetAttribute("ItemTransform")
				p.Name = reader.GetAttribute("Name")
				p.AppliedTrapPreset = reader.GetAttribute("AppliedTrapPreset")
				p.OverrideList = reader.GetAttribute("OverrideList")
				p.AppliedMaster = reader.GetAttribute("AppliedMaster")
				p.MasterPageTransform = reader.GetAttribute("MasterPageTransform")
				p.TabOrder = reader.GetAttribute("TabOrder").Split(" "c)
				p.GridStartingPoint = [Enum].Parse(GetType(GridStartingPointOptions), reader.GetAttribute("GridStartingPoint"))
				p.UseMasterGrid = CBool(reader.GetAttribute("UseMasterGrid"))
			End If

			While reader.Read()
				Select Case reader.Name
					Case "Properties"
						While reader.Read()
							Select Case reader.Name
								Case "PageColor"
									p.PageColor = PageColor.ReadXml(reader)
								Case "Descriptor"
									p.Descriptor = Descriptor.ReadXml(reader)
								Case Else
									If reader.NodeType = XmlNodeType.Element Then
										Debug.WriteLine("Unrecognized element: {0} in element: {1}", reader.Name, "Page - Properties")
									End If
							End Select
							If reader.Name = "Properties" And reader.NodeType = XmlNodeType.EndElement Then Exit While
						End While
					Case "GridDataInformation"
						p.GridDataInformation = GridDataInformation.ReadXml(reader)
					Case "MarginPreference"
						p.MarginPreference = MarginPreference.ReadXml(reader)
					Case "Page"
						If reader.NodeType = XmlNodeType.EndElement Then Exit While
					Case Else
						If reader.NodeType = XmlNodeType.Element Then
							Debug.WriteLine("Unrecognized element: {0} in element: {1}", reader.Name, "Page")
						End If
				End Select
			End While

			Return p
		End Function
	End Class
End Namespace